#include <stdio.h>
#include <list>
#include <string>
#include "mex.h"

using namespace std;

#define MAX_STR_LENGTH 256

void mexFunction( int nlhs,mxArray *plhs[],int nrhs, mxArray *prhs[]) {
	char command[MAX_STR_LENGTH];
    FILE *stream;
    char s[MAX_STR_LENGTH];
    double  *x;

    list<string> vals;
    
    mxGetString(prhs[0],command,sizeof(command));

    if( (stream  = fopen(command, "r" )) == NULL ) {
        mexErrMsgTxt("Unable to open file.\n");
    }
    
    // Retrieve all data
    int i=1;
    while(fscanf(stream, "%s", s)!=EOF) {
        if(i%5!=0)
            vals.push_back(s);
        ++i;
    }
    
    plhs[0] = mxCreateDoubleMatrix(4, vals.size()/4, mxREAL);
    
    x=mxGetPr(plhs[0]);
    
    list <string>::iterator itor;
    
    for(itor=vals.begin(), i=0;itor!=vals.end();++itor, ++i) {
        *x=atof((*itor).c_str());
        ++x;
    }

    fclose(stream);
}